<?php
class UserForm extends Zend_Form
{
    public function __construct ($permissionlist = null, $user = null, $percheck = null)
    {
        parent::__construct();
        $this->addElementPrefixPath('My_Foo', 'My/Foo/');
        $this->addElementPrefixPath('My_Foo_Filter', 'My/Foo/Filter', 'filter');
        $this->addElementPrefixPath('My_Foo_Validate', 'My/Foo/Validate', 'validate');
        $this->addElementPrefixPath('My_Foo_Decorator', 'My/Foo/Decorator', 'decorator');
        $this->setAction(Zend_View_Helper_Url::url(array('controller' => 'acl' , 'action' => 'editusersubmit')));
        $this->setMethod('post');
        $this->setName('User_Form');
        $nameField = new Zend_Form_Element_Text('UserName');
        $nameField->setLabel('UserName');
        $nameField->addValidator('alnum')->setRequired(true)->addFilter('StringToLower');
        $emailField = new Zend_Form_Element_Text('Email');
        $emailField->setLabel('Email');
        $emailField->addValidator('EmailAddress')->setRequired(true)->addFilter('StringToLower');
        $pArr = array();
        $permissionField = new Zend_Form_Element_MultiCheckbox('Permissions');
        $permissionField->setLabel('Permissions');
        foreach ($permissionlist as $permission) {
            $pArr[$permission['id']] = $permission['description'];
        }
        $passwordField = new Zend_Form_Element_Text('Password');
        $passwordField->setLabel('Password');
        $passwordField->addValidator('alnum')->setRequired(true)->addFilter('StringToLower');
        if ($user) {
            $pcArr = array();
            foreach ($percheck as $row) {
                $pArr[$row['id']] = $row['description'];
                array_push($pcArr, $row['id']);
            }
        }
        $permissionField->addMultiOptions($pArr);
        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Submit');
        if ($user) {
            $permissionField->setValue($pcArr);
            $nameField->setValue($user['username']);
            $passwordField->setValue($user['password']);
            $emailField->setValue($user['email']);
        }
        $elements = array($nameField , $emailField ,$passwordField, $permissionField , $submit);
        $this->addElements($elements);
        $this->setDecorators(array('FormElements' , array('HtmlTag' , array('tag' => 'table')) , 'Form'));
        $this->setElementDecorators(array('ViewHelper' , 'Errors' , array(array('data' => 'HtmlTag') , array('tag' => 'td' , 'class' => 'element')) , array('Label' , array('tag' => 'td')) , array(array('row' => 'HtmlTag') , array('tag' => 'tr'))));
        $submit->setDecorators(array('ViewHelper' , array(array('data' => 'HtmlTag') , array('tag' => 'td' , 'class' => 'element'))));
    }
}
